var idSelected;

function logOut(ruta){    
    var xmlhttp;		
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }			
    xmlhttp.open("GET",ruta+"/Logout",true);    
    xmlhttp.send();
}

function crearMaterial(ruta){                    
    var form = document.getElementById("MaterialFormNew");
    form.setAttribute("name","Crear");
    form.setAttribute("method","POST");         
    document.Crear.action=ruta+'/Material?accion=Crear&id=0';
    document.Crear.submit();       
} 

function modificarMaterial(ruta){                    
    var form = document.getElementById("MaterialForm");
    form.setAttribute("name","Modificar");
    form.setAttribute("method","POST");         
    document.Modificar.action=ruta+'/Material?accion=Modificar&id='+idSelected;
    document.Modificar.submit();   
} 

function borrarMaterial(ruta){                    
    var form = document.getElementById("MaterialForm");
    form.setAttribute("name","Borrar");
    form.setAttribute("method","POST");         
    document.Borrar.action=ruta+'/Material?accion=Borrar&id='+idSelected;
    document.Borrar.submit();   
}

function showMaterialInModal(ruta){    
    var xmlhttp,x;		
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }			
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            x = xmlhttp.responseXML.documentElement.getElementsByTagName("Material");                                
            for(i=0;i<x.length;i++){  
                var NameText = document.getElementById("NameMaterial");
                var DescriptionText = document.getElementById("DescriptionMaterial");
                var QuantityText = document.getElementById("QuantityMaterial");
                var PriceText = document.getElementById("PriceMaterial");
                NameText.setAttribute("value",x[i].getElementsByTagName("nombre")[0].firstChild.nodeValue);
                DescriptionText.setAttribute("value",x[i].getElementsByTagName("descripcion")[0].firstChild.nodeValue);                
                QuantityText.setAttribute("value",x[i].getElementsByTagName("cantidad")[0].firstChild.nodeValue);
                PriceText.setAttribute("value",x[i].getElementsByTagName("costo")[0].firstChild.nodeValue);
            }						
        }
    }
    xmlhttp.open("GET",ruta+"/Material?id="+idSelected,true);
    xmlhttp.send();
}
function idClicked(id, ruta){
    idSelected = id;
    showMaterialInModal(ruta);
}

function validarCampos(){
    
    var regexp = new RegExp('^[a-z0-9]{1,10}$');
        
    if(!regexp.test(document.getElementById("NameMaterial")))
        return true;
    else if(!regexp.test(document.getElementById("DescriptionMaterial")))
        return true;
    
    
    
    return false;
         
    
}

function onlyNumbers(evt){
    var keyPressed = (evt.which) ? evt.which : event.keyCode
    return !(keyPressed > 31 && (keyPressed < 48 || keyPressed > 57));
}

function onlyNumbersAndLetters(evt){
    var keyPressed = (evt.which) ? evt.which : event.keyCode
    return !((keyPressed < 48 || keyPressed > 57) &&
            (keyPressed < 65 || keyPressed > 90) &&
            (keyPressed < 97 || keyPressed > 122) &&
            keyPressed != 13
            );
}
function onlyNumbersLettersAndWhiteSpaces(evt){
    var keyPressed = (evt.which) ? evt.which : event.keyCode
    return !((keyPressed < 48 || keyPressed > 57) && 
        (keyPressed < 65 || keyPressed > 90) &&
        (keyPressed < 97 || keyPressed > 122) && keyPressed != 32);
}

function onlyFloat(e,obj){  
  tecla = (document.all) ? e.keyCode : e.which;    
   if (tecla==8) return true;  
  
   Punto = obj.value.split('.');  
    if (Punto.length>=2)  
    {  
        patron =/[0-9]/;  
    }else  
  patron =/[0-9.]/;  
  te = String.fromCharCode(tecla);  
  
     return patron.test(te);   
}  

/* Validación si encuentra espacios en el texto
var regexp = new RegExp('^[a-z0-9]{1,10}$');        
    if(!regexp.test(texto))
        return true;
    else
        return false;
*/


function showUserInModal(id){        
    $.ajax({
        url: '../Usuario?id='+id,
        type: 'get',
        //data: $('#detalleusuario').serialize(),
        success: function(request)
        {                        
            $("#cedulausuario").val(request.cedula);            
            $("#nombreusuario").val(request.nombre);            
        },
        error: function(request)
        {
            alert("Error en la aplicación, por favor contacte al administrador.");
        }
    });
}
function crearUsuario(ruta){          
    /*var form = document.getElementById("detalleusuario");
    form.setAttribute("name","Crear");
    form.setAttribute("method","POST");         
    document.Crear.action=ruta+'/Usuario?accion=Crear&id=0';
    document.Crear.submit();*/
} 

function modificarUsuario(ruta){                    
    var form = document.getElementById("detalleusuario");
    form.setAttribute("name","Modificar");
    form.setAttribute("method","POST");         
    document.Modificar.action=ruta+'/Usuario?accion=Modificar&id='+idSelected;
    document.Modificar.submit();   
} 

function borrarUsuario(ruta){                    
    var form = document.getElementById("detalleusuario");
    form.setAttribute("name","Borrar");
    form.setAttribute("method","POST");         
    document.Borrar.action=ruta+'/Usuario?accion=Borrar&id='+idSelected;
    document.Borrar.submit();   
}
